import { _decorator, Component, game, math, Node, sp, v2, v3 } from 'cc';
import { levelEnd } from './levelEnd';
import { timeBg } from './timeBg';
const { ccclass, property } = _decorator;

@ccclass('levelBgpng')
export class levelBgpng extends Component {

    //四个碎片的位置(x,y,反转，角度)
    polygonsInfo: polygonInfo[] = [
        { x: 83, y: -225, angle: [180, -180], reverse: -1 },//金黄
        { x: 424, y: -225, angle: [180, -180], reverse: -1 },//紫色
        { x: -320, y: -225, angle: [0, 360], reverse: 1 },//橙黄
        { x: 504, y: -225, angle: [90, -270], reverse: 1 },//绿色

    ]


    //绘制时四个顶点的位置
    positions: math.Vec3[] = [
        v3(0, 0, 0),
        v3(512, 0),
        v3(512, 104, 0),
        v3(0, 104, 0)
    ]
    resources: any;


    start() {
        //注册监听
        game.on('gameVictory', this.gameVictory, this)
    }

    //检测拼图位置
    gameVictory() {
        const childrens = this.node.children[1].children;
        let flag = 0;
        console.log(childrens[2].angle, this.polygonsInfo[2].angle[0], this.polygonsInfo[2].angle[1])
        for (let i = 0; i < childrens.length; i++) {
            if (childrens[i].scale.x != this.polygonsInfo[i].reverse) {
                console.log("反转不相等")
                break
            } else if ((childrens[i].angle != this.polygonsInfo[i].angle[0]) && (childrens[i].angle != this.polygonsInfo[i].angle[1])) {
                console.log("角度不相等")
                break
            } else if (childrens[i].getPosition().x != this.polygonsInfo[i].x) {
                console.log("x坐标不相等")
                break
            } else if (childrens[i].getPosition().y != this.polygonsInfo[i].y) {
                console.log("y坐标不相等")
                break
            } else {
                flag++
                console.log('flag=' + flag)
            }

        }

        if (flag == 4) {
            console.log("拼图成功！")
            let timeBgNode = this.node.parent.getChildByName('timeBg');
            timeBgNode.getComponent(timeBg).stopSchedule()
            //播放成功字样的骨骼动画




        }

    }

    //播放骨骼动画
    playSuccess() {
        let successNode = new Node('palyBode');
        successNode.addComponent(sp.Skeleton)
        // 创建 Spine 动画对象
        const skeleton = successNode.addComponent(sp.Skeleton)

        // 加载 Spine 动画数据
        skeleton.skeletonData = this.loadSpineData('uieffect_7/uieffect_7.json'); // 设置资源路径

        // 设置动画并播放
        skeleton.setAnimation(0, 'animation_name', false); // 设定动画名称，并设置为不循环播放（false）

        // 监听动画播放完毕的事件
        skeleton.setCompleteListener((trackEntry) => {
            console.log('动画播放完成！');
            this.onAnimationComplete(); // 动画完成后的回调
        });
    }

    //骨骼动画完成后
    onAnimationComplete() {
        let levelEndNode = this.node.parent.getChildByName('levelEnd');
        levelEndNode.getComponent(levelEnd).starTwen();
    }

    loadSpineData(spineJsonPath: string) {
        const spineData = this.resources.get(spineJsonPath, sp.SkeletonData);
        return spineData;
    }


    protected onDestroy(): void {
        game.off('gameVictory', this.gameVictory, this)
    }



}


interface polygonInfo {
    x: number;
    y: number;
    angle: number[];
    reverse: number;
}




